package com.example.springboot.entity;

import com.alibaba.ttl.TransmittableThreadLocal;
import io.swagger.models.auth.In;

import java.util.Optional;

/**
 * 用户上下文
 */

public final class UserContext {
    private static final ThreadLocal<User> USER_THREAD_LOCAL = new ThreadLocal<>();

    public static User getUser() {
        return USER_THREAD_LOCAL.get();
    }

    public static void setUser(User user) {
        USER_THREAD_LOCAL.set(user);
    }

    public static Integer getUserId() {
        User user = USER_THREAD_LOCAL.get();
        if (user != null) {
            return user.getId();
        }
        return null;
    }

    public static String getUsername() {
        User user = USER_THREAD_LOCAL.get();
        if (user != null) {
            return user.getUsername();
        }
        return null;
    }

    public static String getNickName() {
        User user = USER_THREAD_LOCAL.get();
        if (user != null) {
            return user.getNickname();
        }
        return null;
    }

    public static void removeUser() {
        USER_THREAD_LOCAL.remove();
    }
}
